#!perl

use strict;
use Text::CSV;
use DBI;

sub new_project
{
	my ($props, $saved) = @_;
	my %p;
	$p{gid} =           $props->[0];
	$p{project_level} = $props->[1];
	$p{parent_id} =     $props->[2];
	$p{action_date} =   $props->[3];
	$p{action_time} =   $props->[4];
	$p{route_name} =    $props->[5];
	$p{dev_id} =        $props->[6];
	$p{custom_name} =   $props->[7];
	$p{dev_floor} =     $props->[8];
	$p{contract_type} = $props->[9];
	$p{custom_type} =   $props->[10];
	$p{floor_type} =    $props->[11];

	$p{saved} = $saved;
	my %child;
	my %childname;
	$p{child} = \%child;
	$p{childname} = \%childname;

	return \%p;
}

sub new_route_project
{
	my ($name, $level) = @_;
	my %p;
	$p{gid} =           undef;
	$p{project_level} = $level;
	$p{parent_id} =     undef;
	$p{action_date} =   undef;
	$p{action_time} =   undef;
	$p{route_name} =    $name;
	$p{dev_id} =        undef;
	$p{custom_name} =   undef;
	$p{dev_floor} =     undef;
	$p{contract_type} = undef;
	$p{custom_type} =   undef;
	$p{floor_type} =    undef;

	$p{saved} = 0;
	my %child;
	my %childname;
	$p{child} = \%child;
	$p{childname} = \%childname;

	return \%p;
}

sub new_resource
{
	my ($props, $saved) = @_;
	my %r;
	$r{gid} =            $props->[0];
	$r{resource_level} = $props->[1];
	$r{work_id} =        $props->[2];
	$r{work_title} =     $props->[3];
	$r{work_level} =     $props->[4];
	$r{parent_id} =      $props->[5];
	$r{name} =           $props->[6];
	$r{username} =       $props->[7];
	$r{password} =       $props->[8];
	$r{login_enable} =   $props->[9];

	$r{saved} = $saved;

	return \%r;
}

sub link_projects
{
	my ($roots, $project_list) = @_;
	my %m30;
	my %id30;
	foreach my $p (@$project_list)
	{
		if ($p->{project_level} == 30)
		{
			if (!exists($roots->{$p->{gid}}))
			{
				$roots->{$p->{route_name}} = $p;
				$m30{$p->{route_name}} = $p;
				$id30{$p->{gid}} = $p;
			}
		}
	}
	my %m20;
	my %id20;
	foreach my $p (@$project_list)
	{
		if ($p->{project_level} == 20)
		{
			my $parent_id = $p->{parent_id};
			if (exists($id30{$parent_id}))
			{
				my $parent = $id30{$parent_id};
				$parent->{child}->{$p->{gid}} = $p;
				$parent->{childname}->{$p->{route_name}} = $p;
				$m20{$p->{route_name}} = $p;
				$id20{$p->{gid}} = $p;
			}
			else
			{
				print STDERR "project has no parent: ".$p->{gid}."\n";
			}
		}
	}
	my %m10;
	my %id10;
	foreach my $p (@$project_list)
	{
		if ($p->{project_level} == 10)
		{
			my $parent_id = $p->{parent_id};
			if (exists($id20{$parent_id}))
			{
				my $parent = $id20{$parent_id};
				$parent->{child}->{$p->{gid}} = $p;
				$parent->{childname}->{$p->{route_name}} = $p;
				$m10{$p->{route_name}} = $p;
				$id10{$p->{gid}} = $p;
			}
			else
			{
				print STDERR "project has no parent: ".$p->{gid}."\n";
			}
		}
	}
	my %m0;
	my %id0;
	foreach my $p (@$project_list)
	{
		if ($p->{project_level} == undef)
		{
			my $parent_id = $p->{parent_id};
			if (exists($id10{$parent_id}))
			{
				my $parent = $id10{$parent_id};
				$parent->{child}->{$p->{gid}} = $p;
				$parent->{childname}->{$p->{dev_id}} = $p;
				$m0{$p->{route_name}} = $p;
				$id0{$p->{gid}} = $p;
			}
			else
			{
				print STDERR "project has no parent: ".$p->{gid}."\n";
			}
		}
	}
}

sub link_resources
{
	my ($roots, $resource_list) = @_;
	foreach my $r (@$resource_list)
	{
		my $work_id = $r->{work_id};
		if (exists($roots->{$work_id}))
		{
		}
		else
		{
			$roots->{$work_id} = $r;
		}
	}
}

1;
